import { deepClone } from './tool'
import { propTypeEnum } from '../enums/activity'
/**
 * @description 从类似于home接口这样的配置接口中对 awardName的处理
 * @param item
 * @returns
 */
export function getTemplateAwardName(item: any){
  //type: // 1礼物 2装扮 3道具
  const { type, counts:{ [0]: { min, max } }, data: { name, extra } } = item
  let unit = ''
  if(min === max){
    if(type === 1){
      unit = ''
      if(min === 1){
        return name
      }
    }else if(type === 2){
      unit = '天'
      return `${name}×${min}${unit}`
    }else if(type === 3){
      if(extra === '1' || extra === '2'){
        unit = '步'
        return `${name}${min}${unit}`
      }else{
        return name
      }
    }
  }else{
    return name
  }
}

/**
 * @description 从类似于home接口这样的配置接口中对 awardName的处理, 针对home接口
 * @param item
 * @returns
 */
export function getDrawAwardName(item: any){
  //type: // 1礼物 2装扮 3道具 取外层 type 和 id 同级
  const { type, count, name } = item
  let unit = ''
  if(type === 1 || type === 3){
    if(count === 1){
      return `${name}×${count}`
    }else{
      return `${name}×${count}`
    }
  }else if(type === 2){
    unit = '天'
    return `${name}×${count}${unit}`
  }
}



/**
 * @description 从兑换中取道具名称
 * @param item
 * @returns
 */
export function getExchangeName(item: any){
  return getDrawAwardName(item)
}

const typeIdToName = {
  1: '礼物',
  2: '装扮',
  3: '道具'
}

const subTypeIdToName = {
  1: '挂件',
  2: '座驾',
  3: '房间背景'
}

/**
 * @description 根据type/subType把兑换按策划想要的方式分组
 */
export function groupExchangesByType(exchanges: Array<Record<string, any>>){
  const list = deepClone(exchanges)
  const result = {
  }
  list.forEach((item: any) => {
    const { target: { type, sub_type: subType } } = item
    let groupType = `${type}`
    let groupName = typeIdToName[groupType]
    if(type === propTypeEnum.zhuangban){
      groupType = `${type}-${subType}`
      groupName = subTypeIdToName[subType]
    }
    if(result[groupType]){
      result[groupType].list.push(item)
    }else{
      result[groupType] = {
        name: groupName,
        list: [item]
      }
    }
  })
  return result
}

/**
 * @description 跳转到兑换页面
 */
export function toExchangePage(){
  const { origin } = window.location
  const { APP_ENV } = window
  let href = `${origin}/static/exchange/#/home?fromActivity=true`
  if(APP_ENV === 'production' || !APP_ENV){
    href = `${origin}/campaign/exchange/#/home?fromActivity=true`
  }
  window.location.href = href
}
